import logging
import json
from logging.handlers import RotatingFileHandler
from core.config.config import LogConfig
from typing import Optional


class CustomLogger:
    """A custom logger that formats logs as JSON."""

    logger: logging.Logger

    def __init__(self, logger: logging.Logger):
        self.logger = logger

    def info(self, msg, data: dict = {}):
        self.logger.info(msg, extra={"custom": data})

    def error(self, msg, data: dict = {}):
        self.logger.error(msg, extra={"custom": data})


class CustomLogFormatter(logging.Formatter):
    def format(self, record):
        json_log = {
            "time": self.formatTime(record, self.default_time_format),
            "level": record.levelname,
            "message": record.getMessage(),
            **record.__dict__.get("custom", {}),
        }
        return json.dumps(json_log, ensure_ascii=False)
